package quartz;

import beans.CarInfo;
import beans.QueryParamInfo;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import service.APIService;
import service.SaveLogService;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by yanglq on 2018/4/8.
 */
public class SearchJob implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        QueryParamInfo info = (QueryParamInfo) jobExecutionContext.getJobDetail().getJobDataMap().get("QueryParamInfo");
        //设置查询截止时间为当前时间
        Date currenTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currenTime);
        info.setQueryTimeEnd(dateString);
        APIService apiService = new APIService();
        SaveLogService saveLogService = new SaveLogService();
        try {
            List<CarInfo> carInfos = apiService.getCarAccess(info);
            for (CarInfo inf : carInfos
                    ) {
                saveLogService.saveLog(inf);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        //查询完毕后把查询开始时间设置为上次查询截止时间
        info.setQueryTimeBegin(formatter.format(currenTime));
    }


}
